﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;

namespace UricaoWeb.Datos.ConexionBD
{
    public class OperacionesBD
    {
        //private string _cadena = @"data source=(LocalDB)\v11.0; initial catalog=UricaoBD;integrated security=true";
        public SqlConnection _cn;
        private SqlCommandBuilder _cmb;
        public DataSet _ds = new DataSet();
        public SqlDataAdapter _da;
        public SqlCommand _comando;
        public string stringConexion = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|UricaoBD.mdf;Integrated Security=True";
                                   
            
           public void publicOperacionesBD(string stringConexion)
            {
             _cn = new SqlConnection(stringConexion);
            }   
            
            
        private void Conectar()
        {
            _cn = new SqlConnection(ConfigurationManager.ConnectionStrings["UricaoBD"].ConnectionString);
            
        }

        public OperacionesBD(string stringConexion)
        {
            _cn = new SqlConnection(stringConexion);
        }

	    public OperacionesBD()
	    {
           Conectar();
	    }

        public void Consultar(string sql, string tabla)
        {
            _ds.Tables.Clear();
            _da = new SqlDataAdapter(sql, _cn);
            _cmb = new SqlCommandBuilder(_da);
            _da.Fill(_ds, tabla);
        }

        public DataTable Consultar2(string tabla)
        {
            string _sql = "select * from " + tabla;
            _da = new SqlDataAdapter(_sql, _cn);
            DataSet _dts = new DataSet();
            _da.Fill(_dts, tabla);
            DataTable _dt = new DataTable();
            _dt = _dts.Tables[tabla];
            return _dt;
        }

        
        public bool Eliminar(string tabla, string condicion)
        {
            _cn.Open();
            string sql = "delete from " + tabla + " where " + condicion;
            _comando = new SqlCommand(sql, _cn);
            int _i = _comando.ExecuteNonQuery();
            _cn.Close();
            if (_i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Actualizar(string tabla, string campos, string condicion)
        {
            _cn.Open();
            string sql = "update " + tabla + " set " + campos + " where " + condicion;
            _comando = new SqlCommand(sql, _cn);
            int _i = _comando.ExecuteNonQuery();
            _cn.Close();
            if ( _i > 0 )
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        public bool Insertar(string sql)
        {
            _cn.Open();
            _comando = new SqlCommand(sql, _cn);
            int _i = _comando.ExecuteNonQuery();
            _cn.Close();
            if (_i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}